/**
 * Created by Jackie.Chen on 2016/12/4.
 */

var userList = function () {
    /*基础配置*/
    var el = 'body';
    var validateCong = {
        rules: {
            username: {
                required: true,
                isTrueUser:true,
                remote:{
                    type: "get",
                    url: "/user/verification.do",
                    dataType: "json",
                    cache:false,
                    async:false,
                    data:{
                        account:
                            function() {
                                return $("#username").val();
                            }
                    }
                }
            },
            password: {
                required: true,
                isTruePaw:true
            },
            confimpw:{
                required: true,
                equalTo: "#password"
            },
            email: {
                required: true,
                isTrueEmail:true,
                remote:{
                    type: "get",
                    url: "/user/verification.do",
                    dataType: "json",
                    cache:false,
                    async:false,
                    data:{
                        account:
                            function() {
                                return $("#email").val();
                            }
                    }
                }
            },
            mobile: {
                required: true,
                isTrueTel:true,
                remote:{
                    type: "get",
                    url: "/user/verification.do",
                    dataType: "json",
                    cache:false,
                    async:false,
                    data:{
                        account:
                            function() {
                                return $("#mobile").val();
                            }
                    }
                }
            },
          /*  code:{
                required: true,
            },*/
            checked:{
                required:true
            }
        },
        messages: {
            username: {
                required: "请输入账号",
                remote:"账号已注册"
            },
            password: {
                required: "请输入密码"
            },
            confimpw:{
                required: "请再次输入密码",
                equalTo: "两次密码必须一致"
            },
            email: {
                required: "请输入邮箱",
                remote:"邮箱已注册"
            },
            mobile: {
                required: "请输入手机号",
                remote:"手机号已注册"
            },
           /* code:{
                required: "请输入验证码",
            },*/
            checked:{
                required:"请勾选协议"
            }
        },
        ignore: ".ignore"
    }
    //添加校验规则账号
    jQuery.validator.addMethod("isTrueUser", function(value, element) {
        var username = /^[A-Za-z0-9]{6,16}$/;
        return this.optional(element) || (username.test(value));
    }, "账号由6-16位数字、字母组成！");
    //添加校验规则密码
    jQuery.validator.addMethod("isTruePaw", function(value, element) {
        var password = /^[A-Za-z0-9]{6,16}$/;
        return this.optional(element) || (password.test(value));
    }, "密码由6-16位数字、字母组成！");
    //添加校验规则邮箱
    jQuery.validator.addMethod("isTrueEmail", function(value, element) {
        var email = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((.[a-zA-Z0-9_-]{2,3}){1,2})$/;
        return this.optional(element) || (email.test(value));
    }, "邮箱格式错误！");
    //添加校验规则手机号
    jQuery.validator.addMethod("isTrueTel", function(value, element) {
        var tel = /^1[3|4|5|7|8][0-9]{9}$/;
        return this.optional(element) || (tel.test(value));
    }, "手机号码格式错误！");

    /*注册*/
    function register() {
            $('#modifyForm').validate(validateCong);
            if ($('#modifyForm').valid()) {
                var dataparam = {
                    "username":  $("#username").val(),
                    "password":  $("#password").val(),
                    "mobile": $("#mobile").val(),
                    "email": $("#email").val()
                };
                ajaxCommonFun({
                    type: 'POST',
                    url: '/user/addUser.do',
                    data: dataparam,
                    success: function (t) {
                        if (t.status == 'ERROR') {
                            layer.msg(t.message);
                        } else {
                            layer.msg(t.result);
                            location.href = '/user/login.html';
                        }
                    }
                });
            }
    }

    function goLogin() {
        location.href="/user/login.html";
    }

    /*end分页*/
    /*页面初始化和事件绑定*/
    function bindEvents() {
        $(el).undelegate()
            .delegate('#goLog','click',goLogin)
            .delegate('#register','click',register);
    }

    /*页面初始化*/
    function init() {
        bindEvents();
    }
    /*end 页面初始化*/
    init();
};
